<!DOCTYPE html>

<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <title>Documentation / Learn Vimscript the Hard Way</title>
        <meta name="description" content="">
        <meta name="author" content="Steve Losh">
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->

        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

        <link href="/static/styles/skeleton/base.css" rel="stylesheet" type="text/css" />
        <link href="/static/styles/skeleton/skeleton.css" rel="stylesheet" type="text/css" />
        <link href="/static/styles/skeleton/layout.css" rel="stylesheet" type="text/css" />

        <link href="/static/styles/tango.css" rel="stylesheet" type="text/css" />
        <link href="/static/styles/style.less" rel="stylesheet/less" type="text/css" />

        <script type="text/javascript" src="/static/scripts/less.js"></script>
    </head>

    <body class="">
        <div class="container">
            <header class="sixteen columns">
                <h1><a href="/">Learn Vimscript the Hard Way</a></h1>
            </header>

            
    <section class="nav three columns">
        
<ul>
<li><a href="#documentation">Documentation</a><ul>
<li><a href="#how-documentation-works">How Documentation Works</a></li>
<li><a href="#help-header">Help Header</a></li>
<li><a href="#what-to-document">What to Document</a></li>
<li><a href="#table-of-contents">Table of Contents</a></li>
<li><a href="#sections">Sections</a></li>
<li><a href="#examples">Examples</a></li>
<li><a href="#write">Write!</a></li>
<li><a href="#exercises">Exercises</a></li>
</ul>
</li>
</ul>


        <div class="prevnext">
            
                <a class="prev" href="53.html">&laquo; Prev</a>
            
            
                <a class="next" href="55.html">Next &raquo;</a>
            
        </div>
    </section>

    <section class="content twelve columns offset-by-one">
        <div> 
<h1 id="documentation">Documentation</h1>
<p>Our Potion plugin has a bunch of useful functionality in it, but it won't be
really useful to anyone unless we document it so they know what it can do!</p>
<p>Vim's own documentation is superb.  It's not overly wordy, but it's extremely
thorough.  It's also inspired many plugin authors to document their own plugins
very well, which has resulted in a wonderful culture of strong documentation in
the Vimscript community.</p>
<h2 id="how-documentation-works">How Documentation Works</h2>
<p>When you read a <code>:help</code> topic in Vim you've surely noticed that some things are
highlighted differently than others.  Let's take a look at how this works.</p>
<p>Open up any help topic (such as <code>:help help</code>) and run <code>:set filetype?</code>.  Vim
will display <code>filetype=help</code>.  Now run <code>:set filetype=text</code>, and you'll see that
the highlighting goes away.  <code>:set filetype=help</code> again and it will come back.</p>
<p>It turns out that Vim help files are simply syntax-highlighted text files like
any other file format!  This means you can write your own and get the same
highlighting.</p>
<p>Create a file called <code>doc/potion.txt</code> in your plugin repo.  Vim/Pathogen looks
for files inside <code>doc</code> folders when indexing help topics, so this is where we'll
write the help for our plugin.</p>
<p>Open this file in Vim and run <code>:set filetype=help</code> so you can see the syntax
highlighting as you type.</p>
<h2 id="help-header">Help Header</h2>
<p>The format of help files is a matter of personal taste.  With that said, I'll
talk about one way to structure them that seems to be popular with the modern
Vimscript community.</p>
<p>The first line of the file should contain the filename of the help file,
followed by a one-line description of what the plugin does.  Add the following
as the first line of your <code>potion.txt</code> file:</p>
<pre class="codehilite"><code class="language-text">*potion.txt* functionality for the potion programming language</code></pre>


<p>Surrounding a word with asterisks in a help file creates a "tag" that can be
jumped to.  Run <code>:Helptags</code> to tell Pathogen to rebuild the index of help tags,
and then open a new Vim window and run <code>:help potion.txt</code>.  Vim will open your
help document like any other one.</p>
<p>Next you should put the title of your plugin along with a longer description.
Some authors (including me) like to have a bit of fun with this and use some
ASCII art to spice things up.  Add a nice title section to the <code>potion.txt</code>
file:</p>
<pre class="codehilite"><code class="language-text">*potion.txt* functionality for the potion programming language

                      ___      _   _              ~
                     / _ \___ | |_(_) ___  _ __   ~
                    / /_)/ _ \| __| |/ _ \| '_ \  ~
                   / ___/ (_) | |_| | (_) | | | | ~
                   \/    \___/ \__|_|\___/|_| |_| ~

          Functionality for the Potion programming language.
        Includes syntax highlighting, code folding, and more!</code></pre>


<p>I got those fun letters by running the <code>figlet -f ogre "Potion"</code> command.
<a href="http://www.figlet.org/">Figlet</a> is a great little program for generating ASCII art text.  The <code>~</code>
characters at the end of the lines ensure that Vim doesn't try to highlight or
hide individual characters inside the art.</p>
<h2 id="what-to-document">What to Document</h2>
<p>Next usually comes a table of contents.  First, though, let's decide what we
actually want to document.</p>
<p>When writing documentation for a new plugin I usually start with the following
list of sections and work from there:</p>
<ul>
<li>Introduction</li>
<li>Usage</li>
<li>Mappings</li>
<li>Configuration</li>
<li>License</li>
<li>Bugs</li>
<li>Contributing</li>
<li>Changelog</li>
<li>Credits</li>
</ul>
<p>If the plugin is large and requires an "overview" I'll write an introductory
section that summarizes how things work.  Otherwise I'll skip that and just move
on.</p>
<p>A "usage" section should explain in, general, how the user will actually <em>use</em>
your plugin.  If they'll interact with it through mappings, tell them that.  If
there aren't too many mappings you can simply list them here, otherwise you may
want to create a separate "mappings" section that lists them all.</p>
<p>The "configuration" section should list each and every user-modifiable variable,
along with its effects and its default value.</p>
<p>The "license" section should specify what license the plugin's code is under, as
well as a URL where the user can find the full text of that license.  Don't
include the full text in the actual help file -- most users know what the common
licenses mean and it just clutters things up.</p>
<p>The "bugs" section should be short and sweet.  List any major bugs that you're
aware of but haven't gotten around to fixing, and tell the user how they can
report new bugs they find to you.</p>
<p>If you want your users to be able to contribute bug fixes and features for the
plugin back to you, they'll need to know how to do it.  Should they send a pull
request on GitHub?  On Bitbucket?  Send a patch in an email?  Any/all of the
above?  Include a "contributing" section makes it clear how you prefer to
receive code.</p>
<p>A changelog is a wonderful thing to include so that when users update your
plugin from version X to version Y they can immediately see what changed.  Also,
I highlight recommend you pick a sane versioning scheme like <a href="http://semver.org/">Semantic
Versioning</a> for your plugin and stick to it.  Your users will thank you.</p>
<p>Finally, I like to include a "credits" section to mention my own name, list
other plugins that this one was inspired by, thank contributors, and so on.</p>
<p>This is usually a good starting point.  For more unique plugins you may feel the
need to deviate from this list, and that's completely fine.  There are no hard
and fast rules except the following:</p>
<ul>
<li>Be thorough.</li>
<li>Don't be too wordy.</li>
<li>Take the user on a journey from having no idea what your plugin is to being an
  expert user of it.</li>
</ul>
<h2 id="table-of-contents">Table of Contents</h2>
<p>Now that we have an idea of what sections we'll include, add the following to
the <code>potion.txt</code> file:</p>
<pre class="codehilite"><code class="language-text">====================================================================
CONTENTS                                            *PotionContents*

    1. Usage ................ |PotionUsage|
    2. Mappings ............. |PotionMappings|
    3. License .............. |PotionLicense|
    4. Bugs ................. |PotionBugs|
    5. Contributing ......... |PotionContributing|
    6. Changelog ............ |PotionChangelog|
    7. Credits .............. |PotionCredits|</code></pre>


<p>There are a couple things to note here.  First, the line of <code>=</code> characters will
be syntax highlighted.  You can use these lines to visually divide up sections
of your help document.  You can also use lines of <code>-</code> characters for
subsections, if you want.</p>
<p>The <code>*PotionContents*</code> will create another tag, so a user can type <code>:help
PotionContents</code> to go directly to the table of contents.</p>
<p>Each of the words surrounded by <code>|</code> characters creates a link to a tag.  Users
can place their cursor on the word in the help file and press <code>&lt;c-]&gt;</code> to jump to
the tag, just as if they had typed <code>:help TheTag</code>.  They can also click them
with their mouse.</p>
<p>Vim will hide these <code>*</code> and <code>|</code> characters and syntax highlight them, so the
result will be a nice, pretty table of contents people can use to get to what
they're looking for.</p>
<h2 id="sections">Sections</h2>
<p>You can create section headers like this:</p>
<pre class="codehilite"><code class="language-text">====================================================================
Section 1: Usage                                       *PotionUsage*

This plugin with automatically provide syntax highlighting for
Potion files (files ending in .pn).

It also...</code></pre>


<p>Make sure to create the correct tags with the <code>*</code> characters so that all the
links in your table of contents work properly.</p>
<p>Go ahead and create headers for each section in the table of contents.</p>
<h2 id="examples">Examples</h2>
<p>I could try to go over all the syntax of help files and how to use it, but it's
really a matter of taste.  So instead I'll give you a list of several Vim
plugins with extensive documentation.</p>
<p>For each one, copy the raw source of the documentation into a Vim buffer and set
its filetype to <code>help</code> to see how it renders.  Switch back to <code>text</code> when you
want to see how an effect was created.</p>
<p>You may find it useful to use your Vimscript skills create a key mapping to
toggle the <code>help</code> and <code>text</code> filetypes for the current buffer.</p>
<ul>
<li><a href="https://github.com/sjl/clam.vim/blob/master/doc/clam.txt">Clam</a>, my own plugin for working with shell commands.  It's a pretty short
  example that hits most of the sections I talked about.</li>
<li>The <a href="https://github.com/scrooloose/nerdtree/blob/master/doc/NERD_tree.txt">NERD tree</a>, a file navigation plugin by Scrooloose.  Note the general
  structure, as well as how he summarizes the mappings in an easy-to-read list
  before explaining every one in detail.</li>
<li><a href="https://github.com/tpope/vim-surround/blob/master/doc/surround.txt">Surround</a>, a plugin for handling "surrounding" characters by Tim Pope.
  Note the lack of a table of contents, the different style of section
  headers, and the table column headers.  Figure out how these things were done,
  and decide if you like them.  It's a matter of taste.</li>
<li><a href="https://github.com/sjl/splice.vim/blob/master/doc/splice.txt">Splice</a>, my own plugin for resolving three-way merge conflicts in version
  control systems.  Note how the lists of mappings are formatted, and how I used
  ASCII-art diagrams to explain layouts.  Sometimes a picture really is worth
  a thousand words.</li>
</ul>
<p>Remember that all of the vanilla Vim documentation can also be used as an
example too.  That should give you plenty to study and learn from.</p>
<h2 id="write">Write!</h2>
<p>Now that you've see how some other plugins structured and wrote their
documentation, fill in the sections for your Potion plugin.</p>
<p>If you're not used to writing technical documentation this might be a challenge.
Learning to write certainly isn't simple, but like any other skill it definitely
requires practice, so just go to it!  It doesn't need to be perfect and you can
always improve it later.</p>
<p>Don't be afraid to write something you're not completely sure about and then
throw it away and rewrite it later.  Often just getting <em>something</em> in the
buffer will get your mind in the mood to write.  It'll still be in version
control if you ever want it back any way.</p>
<p>A good way to start is to imagine you've got a friend who also uses Vim sitting
next to you.  They've never used your plugin before but are intrigued, and your
goal is to turn them into an expert user of it.  Thinking about explaining
things to an actual human being will help keep you grounded and avoid getting
too deeply technical before you've established a good overview of how things
work.</p>
<p>If you're still stuck and feel like you're not up to the challenge of writing
the documentation for a full plugin, try doing something smaller.  Pick
a mapping in your <code>~/.vimrc</code> file and document it fully in a comment.  Explain
what it's for, how to use it, and how it works.  For example, this is in my own
<code>~/.vimrc</code> file:</p>
<pre class="codehilite"><code class="language-vim">" "Uppercase word" mapping.
"
" This mapping allows you to press &lt;c-u&gt; in insert mode to convert the
" current word to uppercase.  It's handy when you're writing names of
" constants and don't want to use Capslock.
"
" To use it you type the name of the constant in lowercase.  While
" your cursor is at the end of the word, press &lt;c-u&gt; to uppercase it,
" and then continue happily on your way:
"
"                            cursor
"                            v
"     max_connections_allowed|
"     &lt;c-u&gt;
"     MAX_CONNECTIONS_ALLOWED|
"                            ^
"                            cursor
"
" It works by exiting out of insert mode, recording the current cursor
" location in the z mark, using gUiw to uppercase inside the current
" word, moving back to the z mark, and entering insert mode again.
"
" Note that this will overwrite the contents of the z mark.  I never
" use it, but if you do you'll probably want to use another mark.
inoremap &lt;C-u&gt; &lt;esc&gt;mzgUiw`za</code></pre>


<p>It's much shorter than the documentation for a full plugin, but it's a good
exercise that will help you practice writing.  It's also very helpful for people
reading your <code>~/.vimrc</code> if you put it up on Bitbucket or GitHub.</p>
<h2 id="exercises">Exercises</h2>
<p>Write the documentation for each section of the Potion plugin.</p>
<p>Read <code>:help help-writing</code> for help about writing help.</p>
<p>Read <code>:help :left</code>, <code>:help :right</code>, and <code>:help :center</code> to learn about three
useful commands for getting your ASCII art perfect.</p></div>

        <div class="prevnext">
            
                <a class="prev" href="53.html">&laquo; Previous</a>
            
            
                <a class="next" href="55.html">Next &raquo;</a>
            
        </div>
    </section>


            <footer class="sixteen columns">
                Made by <a href="http://stevelosh.com">Steve Losh</a>.

                <a href="/license.html">License</a>.

                Built with
                <a href="http://bitbucket.org/sjl/bookmarkdown/">Bookmarkdown</a>.
            </footer>
        </div>

        
            <script type="text/javascript">
                var _gaq = _gaq || [];
                _gaq.push(['_setAccount', 'UA-15328874-8']);
                _gaq.push(['_trackPageview']);

                (function() {
                 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                 })();
            </script>
        

        
            <script type="text/javascript">
                var _gauges = _gauges || [];
                (function() {
                 var t   = document.createElement('script');
                 t.type  = 'text/javascript';
                 t.async = true;
                 t.id    = 'gauges-tracker';
                 t.setAttribute('data-site-id', '4e8f83b7f5a1f546e200000d');
                 t.src = '//secure.gaug.es/track.js';
                 var s = document.getElementsByTagName('script')[0];
                 s.parentNode.insertBefore(t, s);
                 })();
             </script>
        
    </body>
</html>
